package ${conf.basePackage}.controllers.${table.camelName};

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Delete;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.annotation.rest.Put;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import ${conf.basePackage}.${table.camelName}.model.${table.className};
import ${conf.basePackage}.${table.camelName}.service.${table.className}Service;
import com.qd.base.controllers.BaseController;
import com.qd.util.ResJsonUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * ${table.comment}
 * @author ${conf.author}
 * @version ${conf.version}
 * @since ${conf.createDate}
 */
@Slf4j
@Path(StringUtils.EMPTY)
public class ${table.className}Controller extends BaseController {

	@Autowired
	private ${table.className}Service ${table.camelName}Service;

	@Get
	public String list(@Param("page") int page, @Param("size") int size) {
		if (page <= 0) page = 1;
		int offset = (page - 1) * size;

		List<${table.className}> ${table.camelName}List = ${table.camelName}Service.find(offset, size);
		if (CollectionUtils.isNotEmpty(${table.camelName}List)) {
			log.debug("list ${table.camelName}, input param = {}, result size : {}", "{page=" + page + ", size=" + size + "}", ${table.camelName}List.size());
		} else {
			log.warn("list ${table.camelName}, input param = {}, result size : 0", "{page=" + page + ", size=" + size + "}");
		}

		return ResJsonUtil.jsonList(CODE_200, SUCCESS_INFO, ${table.camelName}List);
    }

    @Get("{id:[0-9]+}")
    public String show(@Param("id") long id) {
		${table.className} ${table.camelName} = ${table.camelName}Service.findById(id);
		if (${table.camelName} == null) {
			log.warn("show ${table.camelName}, input param = {}, result is null", "{id=" + id + "}");
		}
		return ResJsonUtil.jsonEntity(CODE_200, SUCCESS_INFO, ${table.camelName});
    }

    @Post
    public String add(${table.className} ${table.camelName}) {
		long id = ${table.camelName}Service.save(${table.camelName});
		if (id <= 0) {
			log.warn("add ${table.camelName}, input param = {}, result is failure", "{" + ${table.camelName}.toString() + "}");
			return ResJsonUtil.json(CODE_200, FAILURE_INFO);
		} else {
			log.debug("add ${table.camelName}, input param = {}, result is success", "{" + ${table.camelName}.toString() + "}");
			return ResJsonUtil.json(CODE_200, SUCCESS_INFO);
		}
    }

    @Put
    public String update(${table.className} ${table.camelName}) {
		int count = ${table.camelName}Service.modify(${table.camelName});
		if (count <= 0) {
			log.warn("update ${table.camelName}, input param = {}, result is failure", "{" + ${table.camelName}.toString() + "}");
			return ResJsonUtil.json(CODE_200, FAILURE_INFO);
		} else {
			log.debug("update ${table.camelName}, input param = {}, result is success", "{" + ${table.camelName}.toString() + "}");
			return ResJsonUtil.json(CODE_200, SUCCESS_INFO);
		}
    }

    @Delete("{id:[0-9]+}")
    public String delete(@Param("id") long id) {
		int count = ${table.camelName}Service.remove(id);
		if (count <= 0) {
			log.warn("delete ${table.camelName}, input param = {}, result is failure", "{id=" + id + "}");
			return ResJsonUtil.json(CODE_200, FAILURE_INFO);
		} else {
			log.debug("delete ${table.camelName}, input param = {}, result is success", "{id=" + id + "}");
			return ResJsonUtil.json(CODE_200, SUCCESS_INFO);
		}
    }
}